package in.iqing.view.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import butterknife.Bind;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.base.BaseActivity;
import in.iqing.control.api.ApiHelper;
import in.iqing.control.api.callback.LoginCallback;
import in.iqing.control.helper.IntentHelper;
import in.iqing.control.util.ToastUtil;
import in.iqing.model.bean.AuthResult;
import in.iqing.model.bean.User;
import in.iqing.model.sp.AccountSP;

/**
 * Created by ZuoShu on 15/7/6.
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.mobile_edit)
    EditText mobile;

    @Bind(R.id.password_edit)
    EditText password;

    ProgressDialog loadingDialog;

    private ExtLoginCallback callback;
    public static final int REQUEST_REGISTER = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void onInitialize(Bundle savedInstanceState) {
        super.onInitialize(savedInstanceState);
        callback = new ExtLoginCallback();
    }

    @OnClick(R.id.register)
    public void onRegisterClick(View v) {
        IntentHelper.startActivityForResult(this, RegisterActivity.class, REQUEST_REGISTER);
    }

    @OnClick(R.id.login)
    public void onLoginClick(View v) {
        String mobileString = mobile.getText().toString();
        String passwordString = password.getText().toString();
        if (TextUtils.isEmpty(mobileString)) {
            mobile.setError(getResources().getString(R.string.activity_login_error_empty_mobile));
            return;
        }
        if (TextUtils.isEmpty(passwordString)) {
            password.setError(getResources().getString(R.string.activity_login_error_empty_password));
            return;
        }
        ApiHelper.getSingleton().login(VOLLEY_TAG, mobileString, passwordString, callback);
    }

    @OnClick(R.id.back)
    public void onBackClick(View v) {
        finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_REGISTER:
                if (resultCode == Activity.RESULT_OK) {
                    finish();
                }
                break;
        }
    }

    private class ExtLoginCallback extends LoginCallback {

        @Override
        public void onFinish() {
            super.onFinish();
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }
        }

        @Override
        public void onStart() {
            super.onStart();
            loadingDialog = ProgressDialog.show(
                    getActivity(),
                    null,
                    getString(R.string.activity_login_signining),
                    true,
                    false
            );
        }

        @Override
        public void onFailure(int code, String msg) {
            switch (code) {
                case 403:
                    ToastUtil.show(getContext(), R.string.activity_login_account_error);
                    break;
                case AuthResult.LOGIN_FAILED:
                    ToastUtil.show(getContext(), R.string.common_error_auth_4);
                    break;
                default:
                    ToastUtil.showError(getContext(), code, msg);
                    break;
            }
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }
        }

        @Override
        public void onSuccess(User user) {
            AccountSP.setMobile(user.getMobile());
            AccountSP.setUserName(user.getUsername());
            AccountSP.setUserId(user.getId());
            setResult(Activity.RESULT_OK);
            finish();
        }
    }


}
